package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(readOnly = true)
public class CheckItemServiceImpl implements CheckItemService {


    @Autowired
    CheckItemDao checkItemDao;


    @Transactional(readOnly = false)
    @Override
    public Result add(CheckItem checkItem) {

        Result result=null;

        try{
            checkItemDao.add(checkItem);
            result=new Result(true,MessageConstant.ADD_CHECKITEM_SUCCESS);

        }catch (Exception e){
            result=new Result(false,MessageConstant.ADD_CHECKITEM_FAIL);
            e.printStackTrace();

        }

        return result;
    }

    @Transactional(readOnly = false)
    @Override
    public Result  update(CheckItem checkItem) {

        Result result=null;

        try{
            checkItemDao.update(checkItem);
            result=new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);

        }catch (Exception e){
            result=new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
            e.printStackTrace();

        }

        return result;

    }

    @Transactional(readOnly = false)
    @Override
    public Result delete(Integer id) {

        Result result=null;
        try{

            List<Integer> checkGroupIds = checkItemDao.queryCheckGroupByCheckItemId(id);
            if (checkGroupIds.size()!=0){
                //当前检查项有关联的检查组，不能删除
                result=new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL_CHECKGROUP);
            }else {
                checkItemDao.delete(id);
                result=new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
            }
        }catch (Exception e){
            result=new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public Result queryById(Integer id) {

        Result result=null;

        try{
            CheckItem checkItem = checkItemDao.queryById(id);
            result=new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);

        }catch (Exception e){
            result=new Result(true,MessageConstant.QUERY_CHECKITEM_FAIL);
            e.printStackTrace();
        }

        return result;
    }

    @Override
    public PageResult queryPage(Integer pageCurrent, Integer pageSize) {

        Page<CheckItem> page = PageHelper.startPage(pageCurrent, pageSize);
        checkItemDao.queryAll();
        return  new PageResult(page.getTotal(),page);
    }


    /**
     * 根据项目编码/项目名称查询检查项
     * @param queryString
     * @return
     */
    @Override
    public PageResult queryString(String queryString) {

        List<CheckItem> list= checkItemDao.queryByCodeOrName(queryString);

        return new PageResult((long)list.size(),list);
    }

    @Override
    public List<CheckItem> queryAll() {

        return checkItemDao.queryAll();

    }
}
